package cn.com.css.taxInspection.workflow.service.impl;

import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.entity.GzddmbContentEntity;
import cn.com.css.taxInspection.workflow.entity.GzddmbEntity;
import cn.com.css.taxInspection.workflow.mapper.GzddmbMapper;
import cn.com.css.taxInspection.workflow.service.GzddmbContentService;
import cn.com.css.taxInspection.workflow.service.GzddmbService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("GzddmbService")
public class GzddmbServiceImpl extends ServiceImpl<GzddmbMapper, GzddmbEntity> implements GzddmbService {

    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private GzddmbContentService gzddmbContentService;
    @Resource
    private GzddmbMapper gzddmbMapper;

    @Override
    public DataResult saveGzddmb(GzddmbContentEntity gzddmbContentEntity) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Date date = new Date();
        String surveyId = TsBqUtils.getUUID();
        String surveyTitle = gzddmbContentEntity.getSurveyTitle();
        String surveyContent = gzddmbContentEntity.getOptionsConent();
        JSONArray surveyContentJsonObject = JSON.parseArray(surveyContent);
//		 JSONObject surveyContentJsonObject = JSONArray.parseObject(surveyContent);
        int wjJsonSize = surveyContentJsonObject.size();
        List<GzddmbContentEntity> setGzddmbContentList = new ArrayList<>();
        for (int i = 0; i < wjJsonSize; i++) {
            JSONObject optionsJsonObject = JSONObject.parseObject(surveyContentJsonObject.get(i).toString());
            String optionsType = optionsJsonObject.get("optionsType").toString();
            String name = TsBqUtils.getUUID();
            String optionsTitle = optionsJsonObject.get("optionsTitle").toString();
            String surveyContentId = TsBqUtils.getUUID();
            String _name = i + "_" + optionsType + "_" + name;
            String optionsContent = optionsJsonObject.get("options").toString();
            GzddmbContentEntity setGzddmbContentEntity = new GzddmbContentEntity();
            switch (optionsType) {
                case Constants.SURVEY_TYPE_OPTION_1:
                    // 文本逻辑
                    setGzddmbContentEntity.setId(surveyContentId);// 问卷选项id

                    setGzddmbContentEntity.setSurveyId(surveyId);// 问卷id

                    setGzddmbContentEntity.setOptionsRelation(_name);// 问卷选项id

                    setGzddmbContentEntity.setOptionsTitle(optionsTitle);// 问卷选项标题

                    setGzddmbContentEntity.setOptionsConent(optionsContent);// 问卷选项内容

                    setGzddmbContentEntity.setOptionsType(optionsType);// 选项类型

                    setGzddmbContentEntity.setOptionsNo(String.valueOf(i + 1)); // 选项序号

                    setGzddmbContentEntity.setDeleted(Constants.DELETEED_NOT);
                    setGzddmbContentEntity.setCreateId(currentUserId);
                    setGzddmbContentEntity.setCreateTime(date);
                    setGzddmbContentList.add(setGzddmbContentEntity);
                    break;
                case Constants.SURVEY_TYPE_OPTION_2:
                    // 多选逻辑
                    setGzddmbContentEntity.setId(surveyContentId);// 问卷选项id

                    setGzddmbContentEntity.setSurveyId(surveyId);// 问卷id

                    setGzddmbContentEntity.setOptionsRelation(_name);// 问卷选项id

                    setGzddmbContentEntity.setOptionsTitle(optionsTitle);// 问卷选项标题

                    setGzddmbContentEntity.setOptionsConent(optionsContent);// 问卷选项内容

                    setGzddmbContentEntity.setOptionsType(optionsType);// 选项类型

                    setGzddmbContentEntity.setOptionsNo(String.valueOf(i + 1)); // 选项序号

                    setGzddmbContentEntity.setDeleted(Constants.DELETEED_NOT);
                    setGzddmbContentEntity.setCreateId(currentUserId);
                    setGzddmbContentEntity.setCreateTime(date);
                    setGzddmbContentList.add(setGzddmbContentEntity);
                    break;
                case Constants.SURVEY_TYPE_OPTION_3:
                    setGzddmbContentEntity.setId(surveyContentId);// 问卷选项id

                    setGzddmbContentEntity.setSurveyId(surveyId);// 问卷id

                    setGzddmbContentEntity.setOptionsRelation(_name);// 问卷选项id

                    setGzddmbContentEntity.setOptionsTitle(optionsTitle);// 问卷选项标题

                    setGzddmbContentEntity.setOptionsConent(optionsContent);// 问卷选项内容

                    setGzddmbContentEntity.setOptionsType(optionsType);// 选项类型

                    setGzddmbContentEntity.setOptionsNo(String.valueOf(i + 1)); // 选项序号

                    setGzddmbContentEntity.setDeleted(Constants.DELETEED_NOT);
                    setGzddmbContentEntity.setCreateId(currentUserId);
                    setGzddmbContentEntity.setCreateTime(date);
                    setGzddmbContentList.add(setGzddmbContentEntity);
                    break;
            }
        }

        // 保存数据库
        GzddmbEntity gzddmbEntity = new GzddmbEntity();
        gzddmbEntity.setId(surveyId);
        gzddmbEntity.setTitle(surveyTitle);
        gzddmbEntity.setContent(surveyContent);
        gzddmbEntity.setTaxorgcode(currentDeptId);
        gzddmbEntity.setState("0");
        gzddmbEntity.setDeleted(Constants.DELETEED_NOT);
        gzddmbEntity.setCreateId(currentUserId);
        gzddmbEntity.setCreateTime(date);
        this.save(gzddmbEntity);
        if (!setGzddmbContentList.isEmpty()) {
            for (GzddmbContentEntity _surveyContent : setGzddmbContentList) {
                gzddmbContentService.save(_surveyContent);
            }
        }
        return DataResult.success();
    }

    @Override
    @DS("oracle")
    public void deleteDataById(String id) {
        gzddmbMapper.deleteDataById(id);

    }
}